<?php

namespace apiend\modules\user\models;

use Yii;

/**
 * 积分日志
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class AccountLogSearch extends \common\models\user\UserAccountLog {

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id'], 'required'],
            [['user_id'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function buildQuery() {
        $query = static::findx($this->user_id);
        $query->andWhere([
            'and',
            [
                'or',
                [
                    'change_type' => [
                        static::CHANGE_TYPE_SHOP_CONSUME,
                        static:: CHANGE_TYPE_SHOP_REFUND,
                        static:: CHANGE_TYPE_THIRD_GRANT,
                        static:: CHANGE_TYPE_THIRD_CONSUME,
                    ]
                ],
                [
                    'relation_table' => [
                        'shop',
                        'trade_order',
                    ]
                ],
            ],
        ]);
        $query->orderBy([
            'created_at' => SORT_DESC,
        ]);
        $sql = $query->createCommand(Yii::$app->sghomeDb)->getRawSql();
        Yii::error($sql);
        return $query;
    }

    /**
     * 接口返回基础数据结构
     * {@inheritdoc}
     */
    public function fieldConfigs() {
        return [
            [
                'label' => 'UUID',
                'attribute' => 'unquieId',
            ],
            [
                'class' => 'common\rest\grid\LabelColumn',
                'attribute' => 'change_type',
            ],
            [
                'class' => 'common\rest\grid\CellColumn',
                'label' => '记录信息',
                'cellOptions' => [
                    'title' => 'remark',
                    'label' => ['relationTable', 'relation_table_id'],
                ],
                'headerOptions' => ['width' => '300'],
            ],
            [
                'attribute' => 'change_credit',
                'format' => 'decimal',
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
            [
                'attribute' => 'current_credit',
                'format' => 'decimal',
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
            [
                'attribute' => 'ip',
                'format' => 'ip',
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
            [
                'attribute' => 'relationTable',
                'visible' => false
            ],
            [
                'attribute' => 'created_at',
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
            [
                'attribute' => 'created_at',
                'format' => 'datetimeRow',
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function configs() {
        return [
            'dialog' => [
                'visible' => false,
                'title' => '品牌内容设置'
            ],
            'config' => [
                'filterConfig' => [
                    [
                        'label' => '品牌名称/编码',
                        'prop' => 'keyword',
                        'type' => 'text',
                    ],
                    [
                        'label' => '状态',
                        'prop' => 'statusList',
                        'type' => 'select',
                        'multiple' => false,
                        'data' => [
                            [
                                'name' => '已启用',
                                'id' => 10,
                            ],
                            [
                                'name' => '未启用',
                                'id' => 0,
                            ],
                        ]
                    ]
                ],
                'gridConfig' => $this->options(),
            ]
        ];
    }

}
